package cn.sfrog.project.biz.web.controller;

import cn.sfrog.common.core.controller.BaseController;
import cn.sfrog.common.core.domain.ApiAjaxResult;
import cn.sfrog.common.core.page.TableDataInfo;
import cn.sfrog.common.utils.SecurityUtils;
import cn.sfrog.project.biz.admin.service.*;
import cn.sfrog.project.biz.web.vo.order.*;
import cn.sfrog.project.biz.web.vo.service.AppServiceDetailQueryVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Slf4j
@RestController
@RequestMapping("/api/serviceOrder")
@Api(value = "/api/serviceOrder", tags = "APP服务订单相关")
public class AppServiceOrderController extends BaseController {

    @Autowired
    private IBizServiceOrderService bizServiceOrderService;
    @Autowired
    private IBizHospitalService hospitalService;
    @Autowired
    private IBizServiceOrderEvaluationService bizServiceOrderEvaluationService;
    @Autowired
    private IBizServiceOrderComplaintService bizServiceOrderComplaintService;
    @Autowired
    private IBizServiceOrderDataUploadService bizServiceOrderDataUploadService;



    @ApiOperation(value = "查询服务或套餐的医院列表")
    @GetMapping("/getServiceHospitalList")
    public ApiAjaxResult<List<AppServiceHospitalResultVO>> getServiceHospitalList(AppServiceDetailQueryVO queryVO){
        List<AppServiceHospitalResultVO> hospitalList = hospitalService.selectAppServiceHospitalList(queryVO);
        return ApiAjaxResult.success(hospitalList);
    }

    @ApiOperation(value = "创建订单")
    @PostMapping("/submitServiceOrder")
    public ApiAjaxResult<AppServiceOrderVO> submitServiceOrder(@RequestBody @Validated AppServiceOrderFormVO formVO){
        return ApiAjaxResult.success(bizServiceOrderService.submitServiceOrder(formVO));
    }

    @ApiOperation(value = "获取服务订单列表")
    @GetMapping("/getServiceOrderList")
    public TableDataInfo<List<AppServiceOrderResultVO>> getServiceOrderList(AppServiceOrderQueryVO query){
        startPage();
        List<AppServiceOrderResultVO> orderList =  bizServiceOrderService.selectAppServiceOrderList(query);
        return getDataTable(orderList);
    }

    @ApiOperation(value = "获取服务订单详情")
    @GetMapping("/detail/{id}")
    public ApiAjaxResult<AppServiceOrderVO> getServiceOrderDetail(@PathVariable Long id){
        AppServiceOrderVO orderVO = bizServiceOrderService.selectAppServiceOrderDetail(id);
        return ApiAjaxResult.success(orderVO);
    }

    @ApiOperation(value = "取消订单")
    @PostMapping("/cancel/{id}")
    public ApiAjaxResult cancel(@PathVariable Long id){
        return ApiAjaxResult.success(bizServiceOrderService.cancel(id));
    }

    @ApiOperation(value = "支付订单")
    @PostMapping("/payment/{id}")
    public ApiAjaxResult payment(@PathVariable Long id){
        return ApiAjaxResult.success(bizServiceOrderService.payment(id));
    }

    @ApiOperation(value = "删除订单")
    @PostMapping("/delete/{id}")
    public ApiAjaxResult delete (@PathVariable Long id){
        return ApiAjaxResult.success(bizServiceOrderService.logicDeleteOrder(id));
    }

    @ApiOperation(value = "订单评价")
    @PostMapping("/evaluation")
    public ApiAjaxResult evaluation(@RequestBody @Validated AppServiceOrderEvaluationFormVO form){
        return ApiAjaxResult.success(bizServiceOrderEvaluationService.saveOrderEvaluation(form));
    }

    @ApiOperation(value = "订单投诉")
    @PostMapping("/complaint")
    public ApiAjaxResult complaint(@RequestBody @Validated AppServiceOrderComplaintFormVO form){
        return ApiAjaxResult.success(bizServiceOrderComplaintService.saveOrderComplaint(form));
    }

    @ApiOperation(value = "接单",httpMethod = "POST")
    @PostMapping("/receiveOrder")
    public ApiAjaxResult receiveOrder(@RequestParam Long id){
        return ApiAjaxResult.success(bizServiceOrderService.receiveOrder(id));
    }

    @ApiOperation(value = "订单签到")
    @PostMapping("/signInOrder")
    public ApiAjaxResult signInOrder(@RequestBody @Validated AppSignInOrderFormVO formVO){
        return ApiAjaxResult.success(bizServiceOrderService.signInOrder(formVO));
    }


    @ApiOperation(value = "用户订单|平台投诉列表")
    @GetMapping("/getOrderComplaintList")
    public TableDataInfo<AppOrderComplaintVO> getOrderComplaintList(){
        Long userId = SecurityUtils.getLoginUser().getUser().getUserId();
        startPage();
        List<AppOrderComplaintVO> voList = bizServiceOrderComplaintService.selectOrderComplaintByUserId(userId);
        return getDataTable(voList);
    }


    @ApiOperation(value = "资料上传")
    @PostMapping("/orderDataUpload")
    public ApiAjaxResult orderDataUpload(@RequestBody @Validated AppOrderDataUploadVO form){
        return ApiAjaxResult.success(bizServiceOrderDataUploadService.saveOrderDataUpload(form));
    }

    @ApiOperation(value = "完成服务",httpMethod = "POST")
    @PostMapping("/completeOrder")
    public ApiAjaxResult completeOrder(@RequestParam Long id){
         return ApiAjaxResult.success(bizServiceOrderService.completeOrder(id));
    }
}
